package cn.yunyichina.provider.bone.service.impl;

import cn.yunyichina.provider.bone.dao.VisitTemplateDao;
import cn.yunyichina.provider.bone.dao.VisitTemplateListDao;
import cn.yunyichina.provider.bone.entity.UserVisitList;
import cn.yunyichina.provider.bone.entity.VisitTemplate;
import cn.yunyichina.provider.bone.entity.VisitTemplateList;
import cn.yunyichina.provider.framework.common.PKGenerator;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by zhuxp on 2017/12/4.
 *
 * @param
 * @Descrition
 */
@Log4j
@Service
public class VisitTemplateServiceImpl {

    private SimpleDateFormat simpleDateFormat  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static Logger logger = LoggerFactory.getLogger(VisitTemplateServiceImpl.class);

    @Autowired
    private VisitTemplateDao visitTemplateDao;
    @Autowired
    private VisitTemplateListDao visitTemplateListDao;

    /**
     * yyt.bone.visit.template.list
     * 查询模板列表
     * @param data
     * @return
     */
    public Response listVisitTemplate(String data){
        Response response = new Response();
        try {
            VisitTemplate visitTemplate = JsonUtils.parseObject(data,VisitTemplate.class);
            if (StringUtils.isBlank(visitTemplate.getDoctorId())||StringUtils.isBlank(visitTemplate.getOrgId())){
                response.setResultCodeAndMessage("-2","医生id或医生orgId不能为空!");
                return response;
            }
            List<VisitTemplate> visitTemplates = visitTemplateDao.queryTemplateList(visitTemplate);
            response.setResultCodeAndMessageAndResult("0","查询模板列表成功",JsonUtils.toJsonString(visitTemplates));
        }catch (Exception e){
           log.error("查询随访模板列表异常",e);
            response.setResultCodeAndMessage("-1","查询随访模板列表异常!");
        }
        return response;
    }

    /**
     * yyt.bone.visit.template.insert
     * 新增随访模板
     * @param data
     * @return
     */
    public  Response insertVisitTemplate(String data){
        Response response = new Response();
        logger.info("新增随访模板,调用insertVisitTemplate 入参:"+JsonUtils.toJsonString(data));
        try {
            VisitTemplate visitTemplate = JsonUtils.parseObject(data,VisitTemplate.class);
            if (StringUtils.isBlank(visitTemplate.getDoctorId())||StringUtils.isBlank(visitTemplate.getOrgId())){
                response.setResultCodeAndMessage("-2","医生id或医生orgId不能为空!");
                return response;
            }
            visitTemplate.setId(PKGenerator.generateId());
            visitTemplate.setUpdateTime(simpleDateFormat.format(new Date()));
            visitTemplate.setCreateTime(simpleDateFormat.format(new Date()));
            JSONObject jsonObject = JSONObject.parseObject(data);
            Object obj = jsonObject.get("template_lists");
            if(obj!=null){
                List<VisitTemplateList> visitTemplateLists = JsonUtils.parseArray(obj.toString(),VisitTemplateList.class);
                for(VisitTemplateList entity:visitTemplateLists){
                    entity.setCreateTime(simpleDateFormat.format(new Date()));
                    entity.setUpdateTime(simpleDateFormat.format(new Date()));
                    entity.setVisitTime(simpleDateFormat.format(new Date(Long.valueOf(entity.getVisitTime()))));
                    entity.setId(PKGenerator.generateId());
                    entity.setTemplateId(visitTemplate.getId());
                    entity.setEnableFlag(1);
                    visitTemplateListDao.insert(entity);
                }
            }
            visitTemplateDao.insert(visitTemplate);
            response.setResultCodeAndMessage("0","新增随访模板成功!");
        }catch (Exception e){
            logger.error("新增随访模板异常!",e);
            response.setResultCodeAndMessage("-1","新增随访模板异常!");
        }
        return  response;
    }

    /**
     * yyt.bone.visit.template.delete
     * 删除模板
     * @param data
     * @return
     */
    public Response deleteVisitTemplate(String data){
        logger.info("请求删除随访模板 deleteVisitTemplate请求入参:"+data);
        Response response = new Response();
        try {
            VisitTemplate visitTemplate = JsonUtils.parseObject(data,VisitTemplate.class);
            if(StringUtils.isBlank(visitTemplate.getId())){
                response.setResultCodeAndMessage("-2","模板id不能为空！");
                return response;
            }
            VisitTemplateList visitTemplateList = new VisitTemplateList();
            visitTemplateList.setTemplateId(visitTemplate.getId());
            List<VisitTemplateList> templateLists = visitTemplateListDao.select(visitTemplateList);
            for (VisitTemplateList entity:templateLists){
                visitTemplateListDao.delete(entity);
            }
            visitTemplateDao.delete(visitTemplate);
            response.setResultCodeAndMessage("0","删除随访模板成功!");
        }catch (Exception e){
            logger.error("删除随访模板异常!",e);
            response.setResultCodeAndMessage("-1","删除随访模板异常!");
        }
        return  response;
    }

    /**
     * yyt.bone.visit.template.update
     * 更新模板
     * @param data
     * @return
     */
    public Response updateVisitTemplate(String data){
        logger.info("更新模板 请求updateVisitTemplate入参:"+data);
        Response response = new Response();
        try {
            VisitTemplate visitTemplate = JsonUtils.parseObject(data,VisitTemplate.class);
            if(StringUtils.isBlank(visitTemplate.getId())){
                response.setResultCodeAndMessage("-2","模板id不能为空！");
                return response;
            }
            JSONObject jsonObject = JSONObject.parseObject(data);
            List<VisitTemplateList> visitTemplateLists = JsonUtils.parseArray(jsonObject.get("template_lists").toString(),VisitTemplateList.class);
            for(VisitTemplateList entity:visitTemplateLists){
                //前端传时间戳 转换时间格式
                if(entity.getVisitTime()!=null){
                    entity.setVisitTime(simpleDateFormat.format(new Date(Long.valueOf(entity.getVisitTime()))));
                }
                if(entity.getId()==null){
                    entity.setId(PKGenerator.generateId());
                    entity.setTemplateId(visitTemplate.getId());
                    entity.setUpdateTime(simpleDateFormat.format(new Date()));
                    entity.setCreateTime(simpleDateFormat.format(new Date()));
                    entity.setEnableFlag(1);
                    visitTemplateListDao.insert(entity);
                }else {
                    entity.setUpdateTime(simpleDateFormat.format(new Date()));
                    visitTemplateListDao.update(entity);
                }
            }
            visitTemplateDao.update(visitTemplate);
            response.setResultCodeAndMessage("0","更新模板成功!");
        }catch (Exception e){
            logger.error("更新模板异常",e);
            response.setResultCodeAndMessage("-1","更新模板异常!");
        }
        return response;
    }

}
